package View.JTree;

import java.io.File;

import javax.swing.tree.DefaultMutableTreeNode;

import Management.Animation;
import Management.AnimationRoute;
import View.JTree.JFileTree.JFileTreeNode;

public class JAnimationTreeNode extends DefaultMutableTreeNode {
	AnimationRoute		animatorRoute;
	private boolean 	isDirectory;
	private boolean 	isList;
	private boolean 	shouldShow = false;
	File				file;
	

	public JAnimationTreeNode(AnimationRoute animatorRoute) {
        this.animatorRoute = animatorRoute;
        this.file		   = animatorRoute.getFile();
        isDirectory = file.isDirectory();
        isList		= animatorRoute.isList();
        if (isDirectory) {
    	    for (File f: animatorRoute.getListFile()) {
    	    	if (f.isDirectory()) {
    	    		shouldShow = true;
    	    		break;
    	    	}
    	    }
        }

        setUserObject(animatorRoute);
    }
	
    public AnimationRoute getAnimatorRoute() {
		return animatorRoute;
	}

	public boolean isDirectory() {
		return isDirectory;
	}

	public File getFile() {
		return file;
	}

    public boolean isList() {
		return isList;
	}
	
    @Override
    public boolean isLeaf() {
        if (file == null || !file.exists()) {
            return false;
        }
        return !isDirectory;
    }

    @Override
    public String toString() {
    	return file.getName();
    }

	@Override
    public boolean getAllowsChildren() {
        if (isDirectory) {
        	return shouldShow;
        }
        else {
        	return false;
        }
    }

	 /**
	 * 节点展开
	 * @return 展开是否含有子节点
	 */
	 public boolean expand() {
        this.removeAllChildren();
        System.out.println(shouldShow);
        if (getAllowsChildren()) {
        	for (File f : file.listFiles()) {
        		if (f == null || !file.exists()) {
        			break;
        		}
        		this.add(new JAnimationTreeNode( new AnimationRoute(f.getPath())));
        	}
        	return true;
        }
        else {
        	return false;
        }
    }
}
